package com.winway.sixbook.service;

import java.util.ArrayList;
import java.util.List;

import com.winway.sixbook.bean.RecordData;
import com.winway.sixbook.bean.SeedData;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class RecordDataManager {

	private static final String TAG = "RecordDataManager";
	private static final String DB_NAME = "sixbook";
	private static final String TABLE_NAME = "gggrecord";
	public static final String  ID = "_id";

	public static final String USER_ID = "user_id";
	public static final String SILENT = "silent";
	public static final String VIBRATE = "vibrate";
	public static final String CONTENT_ZM = "content_zm";
	public static final String TYPE_ID = "type_id";
	public static final String TYPE_NAME = "type_name";
	public static final String ADD_TIME = "add_time";
	public static final String TIME_SLOT = "time_slot";
	public static final String ADD_HOUR = "add_hour";
	public static final String ADD_MIN = "add_min";
	public static final String CONTENT_FM = "content_fm";
	public static final String CONTENT_WY = "content_wy";
	public static final String ISQY = "isqy";
	public static final String ADD_SEC = "add_sec";
	public static final String ISDO = "isdo";

	private Context mContext = null;
	private SQLiteDatabase mSQLiteDatabase = null;
	private DataBaseManagementHelper mDatabaseHelper = null;

	public RecordDataManager(Context context) {
		mContext = context;
		Log.i(TAG, "RecordDataManager construction!");
	}

	public void openDataBase() throws SQLException {
		
		mDatabaseHelper = DataBaseManagementHelper.getInstance(mContext);
		mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
	}

	public void closeDataBase() throws SQLException {
		mDatabaseHelper.close();
	}

	public long insertRecordData(RecordData recordData) {
		
		
		Integer userid = recordData.getUserId();
		String content_zm = recordData.getContent_zm();
		Integer typeid = recordData.getTypeid();
		String typename = recordData.getTypename();
		String addtime = recordData.getAddtime();
		Integer timeslot = recordData.getTimeslot();
		Integer addhour = recordData.getAddhour();
		Integer addmin = recordData.getAddmin();
		String content_fm = recordData.getContent_fm();
		String content_wy = recordData.getContent_wy();
		Integer isqy = recordData.getIsqy();
		Integer addsec = recordData.getAddsec();

		ContentValues values = new ContentValues();
		values.put(USER_ID, ""+userid);
		values.put(CONTENT_ZM, content_zm);
		values.put(TYPE_ID, ""+typeid);
		values.put(TYPE_NAME, typename);
		values.put(ADD_TIME, addtime);
		values.put(TIME_SLOT, timeslot);
		values.put(ADD_HOUR, addhour);
		values.put(ADD_MIN, addmin);
		values.put(CONTENT_WY, content_wy);
		values.put(CONTENT_FM, content_fm);
		values.put(ISQY, isqy);
		values.put(ADD_SEC, addsec);
		return mSQLiteDatabase.insert(TABLE_NAME, ID, values);
	}

	public boolean updateRecordData(RecordData recordData) {

		int id = recordData.getRecordId();
		Integer userid = recordData.getUserId();
		String content_zm = recordData.getContent_zm();
		Integer typeid = recordData.getTypeid();
		String typename = recordData.getTypename();
		String addtime = recordData.getAddtime();
		Integer timeslot = recordData.getTimeslot();
		Integer addhour = recordData.getAddhour();
		Integer addmin = recordData.getAddmin();
		String content_fm = recordData.getContent_fm();
		String content_wy = recordData.getContent_wy();
		Integer isqy = recordData.getIsqy();
		Integer addsec = recordData.getAddsec();

		ContentValues values = new ContentValues();
		values.put(USER_ID, ""+userid);
		values.put(CONTENT_ZM, content_zm);
		values.put(TYPE_ID, ""+typeid);
		values.put(TYPE_NAME, typename);
		values.put(ADD_TIME, addtime);
		values.put(TIME_SLOT, timeslot);
		values.put(ADD_HOUR, addhour);
		values.put(ADD_MIN, addmin);
		values.put(CONTENT_WY, content_wy);
		values.put(CONTENT_FM, content_fm);
		values.put(ISQY, isqy);
		values.put(ADD_SEC, addsec);
		
		return mSQLiteDatabase.update(TABLE_NAME, values, ID + "=" + id, null) > 0;
	}

	public Cursor fetchRecordData(int id) throws SQLException {

		Cursor mCursor = mSQLiteDatabase.query(false, TABLE_NAME, null, ID
				+ "=" + id, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public Cursor fetchAllRecordDatas() {
		return mSQLiteDatabase.query(TABLE_NAME, null, null, null, null, null,null);
	}

	public boolean deleteRecordData(int id) {
		return mSQLiteDatabase.delete(TABLE_NAME, ID + "=" + id, null) > 0;
	}

	public boolean deleteAllRecordDatas() {
		return mSQLiteDatabase.delete(TABLE_NAME, null, null) > 0;
	}


	public String getStringByColumnName(String columnName, int id) {
		Cursor mCursor = fetchRecordData(id);
		int columnIndex = mCursor.getColumnIndex(columnName);
		String columnValue = mCursor.getString(columnIndex);
		mCursor.close();
		return columnValue;
	}

	public boolean updateRecordDataById(String columnName, int id, String columnValue) {
		ContentValues values = new ContentValues();
		values.put(columnName, columnValue);
		return mSQLiteDatabase.update(TABLE_NAME, values, ID + "=" + id, null) > 0;
	}
	
	public Cursor findRecordByTypeid(String typeid){
		Log.i(TAG,"findRecordByTypeid , typeid="+typeid);
		return mSQLiteDatabase.query(TABLE_NAME, null, TYPE_ID+"="+typeid, null, null, null, null);
	}
	
	public Cursor findRecordByTimeslot(String addtime,String addhour,String addmin){
		Log.i(TAG,"findRecordByTimeslot , addhour="+addhour +" , addmin="+addmin);
		String sql = "select * from (select _id recorddataid,content_zm content,'1' contenttype,type_name,type_id,isqy  from gggrecord where add_time='"+addtime
				+"' and add_hour='"+addhour+"' and add_min='"+addmin+"'" +
				" union " +
				" select _id recorddataid,content_fm content,'2' contenttype,type_name,type_id,isqy  from gggrecord where add_time='"+addtime
				+"' and add_hour='"+addhour+"' and add_min='"+addmin+"'" +
				" union " +
				" select _id recorddataid,content_wy content,'3' contenttype,type_name,type_id,isqy  from gggrecord where add_time='"+addtime
				+"' and add_hour='"+addhour+"' and add_min='"+addmin+"' ) t" +
				" order by t.contenttype";
//		Log.i(TAG,sql);
		return mSQLiteDatabase.rawQuery(sql, null);
	}
	
	public Cursor findRecordByAddtime(String addtime){
		Log.i(TAG,"findRecordByTime , ADD_TIME="+addtime);
//		return mSQLiteDatabase.query(TABLE_NAME, null, ADD_TIME+"="+addtime, null, null, null, null);
		return mSQLiteDatabase.rawQuery("select * from gggrecord where add_time='"+addtime+"' order by add_hour,add_min asc", null);
	}
	
	public Cursor findRecord(){
		Log.i(TAG,"findRecord ");
//		return mSQLiteDatabase.query(TABLE_NAME, null, ADD_TIME+"="+addtime, null, null, null, null);
		return mSQLiteDatabase.rawQuery("select * from gggrecord order by add_time,add_hour,add_min asc", null);
	}
	
	public Cursor findRecordByTimeandType(String startstr, String endstr, String typename){
		Log.i(TAG,"findRecordByTimeandType");
		String sql="select * from gggrecord where ADD_TIME>='"+startstr+"' and ADD_TIME<='"+endstr+"' and type_name='"+typename+"' order by add_time,add_hour,add_min asc";
		if("所有类型".equals(typename)){
			sql="select * from gggrecord where ADD_TIME>='"+startstr+"' and ADD_TIME<='"+endstr+"' order by add_time,add_hour,add_min asc";
		}
		return mSQLiteDatabase.rawQuery(sql, null);
	}
	

	public int findSeedsCount(){
		int result=0; 
		Cursor mCursor=mSQLiteDatabase.rawQuery("select * from seeds ", null);
		if(mCursor!=null){
			result=mCursor.getCount();
			mCursor.close();
			Log.i(TAG,"findSeedsCount , result="+result);
		}
		return result;
	}
	
	public String findSeedIdByName(String seedName){
		Log.v(TAG, seedName);
		String result=""; 
		List<SeedData> list = new ArrayList<SeedData>();
		SeedData seedData = null ;
		Cursor cursor = mSQLiteDatabase.query("seeds", null, null, null, null, null, null);
		if(null != cursor){
            if(cursor.moveToFirst() ){
                do{
                	seedData =new SeedData();
                	seedData.setId(cursor.getString(cursor.getColumnIndex("_id")));
                	seedData.setSeedName(cursor.getString(cursor.getColumnIndex("seed_name")));
                	list.add(seedData);
                }while( cursor.moveToNext());
            }
		}
		if(null !=list && list.size()>0){
			seedData = list.get(0);
			if(null != seedData){
				result = seedData.getId();
			}
		}
		return result;
	}
	
	public String[] fetchAllSeedNames() {
		
		List<String> list = new ArrayList<String>();
		SeedData seedData = null ;
		Cursor cursor = mSQLiteDatabase.query("seeds", null, null, null, null, null, null);
		if(null != cursor){
            if(cursor.moveToFirst() ){
                do{
                	list.add(cursor.getString(cursor.getColumnIndex("seed_name")));
                }while( cursor.moveToNext());
            }
		 }
		return  list.toArray(new String[list.size()]) ;
	}
	
	public boolean updateRecordData(String id,String str1,String str2,String str3,String str4) {
		ContentValues values = new ContentValues();
		values.put(CONTENT_ZM, str1);
		values.put(CONTENT_FM, str2);
		values.put(CONTENT_WY, str3);
		values.put(ISDO, str4);
		return mSQLiteDatabase.update(TABLE_NAME, values, ID + "=" + id, null) > 0;
	}
	
	public int findTodayQyCount(String datestr){
		int result=0; 
		Cursor mCursor=mSQLiteDatabase.rawQuery("select * from "+ TABLE_NAME +" where isqy =1 and add_time='"+datestr+"'" , null);
		if(mCursor!=null){
			result=mCursor.getCount();
			mCursor.close();
			Log.i(TAG,"findTodayQyCount , result="+result);
		}
		return result;
	}
	
	public String findTodayQyId(String datestr){
		String qyid = "0";
		Cursor mCursor=mSQLiteDatabase.rawQuery("select * from "+ TABLE_NAME +" where isqy =1 and add_time='"+datestr+"'" , null);
		if (mCursor != null) {
			mCursor.moveToFirst();
			qyid = mCursor.getString(mCursor.getColumnIndex("_id"));
		}
		return qyid;
	}
	
	public int findCountTableCount(){
		int result=0; 
		Cursor mCursor=mSQLiteDatabase.rawQuery("select * from counttable ", null);
		if(mCursor!=null){
			result=mCursor.getCount();
			mCursor.close();
			Log.i(TAG,"findCountTableCount , result="+result);
		}
		return result;
	}
	
	public int findZuiHaoCount(String datestr){
		int result=0; 
		Cursor mCursor=mSQLiteDatabase.rawQuery("select * from "+ TABLE_NAME +" where type_id =0 and add_time='"+datestr+"'" , null);
		if(mCursor!=null){
			result=mCursor.getCount();
			mCursor.close();
			Log.i(TAG,"findTodayQyCount , result="+result);
		}
		return result;
	}
	
	public String findZuiHaoId(String datestr){
		String qyid = "0";
		Cursor mCursor=mSQLiteDatabase.rawQuery("select * from "+ TABLE_NAME +" where type_id =0 and add_time='"+datestr+"'" , null);
		if (mCursor != null) {
			mCursor.moveToFirst();
			qyid = mCursor.getString(mCursor.getColumnIndex("_id"));
		}
		return qyid;
	}
	
	public Cursor findCommonRecordByTime(String addtime){
		Log.i(TAG,"findRecordByTimeAndType , ADD_TIME="+addtime );
		return mSQLiteDatabase.rawQuery("select * from gggrecord where type_id <>0 and add_time='"+addtime+"' order by add_hour,add_min,add_sec asc", null);
	}
	
	public Cursor findZuihaoRecord(String addtime){
		return mSQLiteDatabase.rawQuery("select * from gggrecord where type_id =0 and add_time='"+addtime+"' order by add_hour,add_min asc", null);
	}
	
	public int findCommonRecordCount(String datestr){
		int result=0; 
		Cursor mCursor=mSQLiteDatabase.rawQuery("select * from "+ TABLE_NAME +" where type_id <>0 and add_time='"+datestr+"'" , null);
		if(mCursor!=null){
			result=mCursor.getCount();
			mCursor.close();
			Log.i(TAG,"findCommonRecordCount , result="+result);
		}
		return result;
	}
	
	public int findZuihaoRecordCount(String datestr){
		int result=0; 
		Cursor mCursor=mSQLiteDatabase.rawQuery("select * from "+ TABLE_NAME +" where type_id=0 and add_time='"+datestr+"'" , null);
		if(mCursor!=null){
			result=mCursor.getCount();
			mCursor.close();
			Log.i(TAG,"findZuihaoRecordCount , result="+result);
		}
		return result;
	}
	
	public int findRecordCountByAddtime(String addtime){
		Log.i(TAG,"findRecordCountByAddtime , ADD_TIME="+addtime);
		
		int result=0; 
		Cursor mCursor=mSQLiteDatabase.rawQuery("select * from gggrecord where add_time='"+addtime+"' order by add_hour,add_min asc" , null);
		if(mCursor!=null){
			result=mCursor.getCount();
			mCursor.close();
			Log.i(TAG,"findZuihaoRecordCount , result="+result);
		}
		return result;
	}
}
